package com.heitian.ssm.service.impl;

import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.dao.AdminEntityMapper;
import com.heitian.ssm.model.AdminEntity;
import com.heitian.ssm.model.UserEntity;
import com.heitian.ssm.service.AdminLoginService;
import com.heitian.ssm.utils.FastJsonUtils;
import com.heitian.ssm.utils.MD5;
import com.heitian.ssm.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-22
 * Time: 23:31
 * Des:
 */

@Service("adminLoginService")
@Transactional(rollbackFor = Exception.class)
public class AdminLoginServiceImpl implements AdminLoginService{
    private Logger log = Logger.getLogger(UserLoginServiceImpl.class);
    @Resource
    private AdminEntityMapper adminEntityMapper;
    public AdminEntity login(String name, String passwd) {
        log.info("入参姓名"+name);
        if(StringUtils.isBlank(name) || StringUtils.isBlank(passwd)){
            return null;
        }
        String passedSalt=null;
        try {
            passedSalt= MD5.md5(passwd, WebConstants.SALT);
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
        }
        log.info("账号"+name+"密码"+passedSalt);
        AdminEntity adminEntity=new AdminEntity();
        adminEntity.setAdminName(name);
        adminEntity.setAdminPwd(passedSalt);
        AdminEntity userEntity1= adminEntityMapper.selectForLogin(adminEntity);
        log.info("出参信息"+ FastJsonUtils.toJSONString(userEntity1));
        return userEntity1;
    }

    public int register(AdminEntity userEntity) {
        if(userEntity == null){
            return 0;
        }
        return adminEntityMapper.insertSelective(userEntity);
    }
}
